package com.seu.me.zookeeper.curator.watcher;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.NodeCache;
import org.apache.curator.framework.recipes.cache.NodeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @author liangfeihu
 * @since 2017/12/5 20:38.
 */
public class CuratorWatcher1 {

    /**
     * zookeeper地址
     */
    static final String CONNECT_ADDR = "192.168.169.129:2181,192.168.169.130:2181,192.168.169.131:2181";
    /**
     * session超时时间 ms
     */
    static final int SESSION_OUTTIME = 5000;

    /**
     * main 测试
     *
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        //1 重试策略：初试时间为1s 重试10次
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
        //2 通过工厂创建连接
        CuratorFramework cf = CuratorFrameworkFactory.builder()
                .connectString(CONNECT_ADDR)
                .sessionTimeoutMs(SESSION_OUTTIME)
                .retryPolicy(retryPolicy)
                .build();

        //3 建立连接
        cf.start();

        //4 建立一个cache缓存
        final NodeCache cache = new NodeCache(cf, "/super1", false);
        cache.start(true);
        cache.getListenable().addListener(new NodeCacheListener() {
            /**
             * 触发事件为创建节点和更新节点，在删除节点的时候并不触发此操作。
             */
            @Override
            public void nodeChanged() throws Exception {
                System.out.println("路径为：" + cache.getCurrentData().getPath());
                System.out.println("数据为：" + new String(cache.getCurrentData().getData()));
                System.out.println("状态为：" + cache.getCurrentData().getStat());
                System.out.println("---------------------------------------");
            }

        });

        Thread.sleep(1000);
        cf.create().forPath("/super1", "123".getBytes());

        Thread.sleep(1000);
        cf.setData().forPath("/super1", "456".getBytes());

        Thread.sleep(1000);
        cf.delete().forPath("/super1");

        Thread.sleep(Integer.MAX_VALUE);

    }
}
